from fastapi import FastAPI
import uvicorn

from apps.ShopService.urls import shop
from apps.UserService.urls import user
from apps.FileService.urls import file

from fastapi.staticfiles import StaticFiles

from fastapi.exceptions import RequestValidationError
from fastapi.responses import PlainTextResponse
from starlette.exceptions import HTTPException as StarHTTPException




app = FastAPI(
    title="FAST API Tutorial",
    description="FAST API基本用法",
    version="1.0.0",
    docs_url="/docs",

)

# 重写HTTPException处理器
@app.exception_handler(StarHTTPException)
async def http_exception(request, exc):
    """
    :param request: 这个参数不能省
    :param exc:
    :return:
    """

    return PlainTextResponse(str(exc.detail), status_code=exc.status_code)

# 重写request参数处理器
@app.exception_handler(RequestValidationError)
async def http_exception(request, exc):
    """
    :param request: 这个参数不能省
    :param exc:
    :return:
    """

    return PlainTextResponse(str(exc), status_code=400)


app.mount(path="/static", app=StaticFiles(directory="static"), name="static")

app.include_router(shop, prefix="", tags=["1.FAST_API_参数"],)
app.include_router(user, prefix="", tags=["2.请求体和状态码"],)
app.include_router(file, prefix="", tags=["3.文件的上传和下载"],)



if __name__ == "__main__":
    uvicorn.run("main:app", port=8080, reload=True,debug=True, workers=2)
